package View.SpinBlurDialog;

import View.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class SpinBlurDialog extends JDialog implements ActionListener {
    MainFrame parentFrame;
    JTextField jtfAngle = new JTextField();
    JTextField jtfIterations = new JTextField();

    JButton jbtnOK = new JButton("OK");
    JButton jbtnCancel = new JButton("Cancel");

    boolean closeFlag;

    static final String OK_COMMAND = "OK_COMMAND";
    static final String CANCEL_COMMAND = "CANCEL_COMMAND";

    float angle;
    int iteration;

    public SpinBlurDialog(MainFrame parentFrame) {
        this.parentFrame = parentFrame;
        setLocationRelativeTo(parentFrame);
        setSize(250, 170);
        setResizable(false);
        setTitle("Spin Blur parameters");
        setModal(true);
        setVisible(false);



        jbtnOK.addActionListener(this);
        jbtnOK.setActionCommand(OK_COMMAND);

        jbtnCancel.addActionListener(this);
        jbtnCancel.setActionCommand(CANCEL_COMMAND);

        GridBagLayout gb = new GridBagLayout();
        setLayout(gb);

        JLabel jlAngle = new JLabel("Set angle (grad): ");
        JLabel jlIterations = new JLabel("Set number of iterations: ");
        gb.setConstraints(jlAngle, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 0), 0, 0));
        add(jlAngle);
        gb.setConstraints(jtfAngle, new GridBagConstraints(0, 1, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jtfAngle);

        gb.setConstraints(jlIterations, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 0), 0, 0));
        add(jlIterations);
        gb.setConstraints(jtfIterations, new GridBagConstraints(0, 3, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jtfIterations);

        gb.setConstraints(jbtnOK, new GridBagConstraints(0, 4, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
        add(jbtnOK);
        gb.setConstraints(jbtnCancel, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
        add(jbtnCancel);

    }

    public boolean runDialog() {
        closeFlag = false;
        setVisible(true);
        return closeFlag;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals(OK_COMMAND)) {
            boolean errFlag = false;
            try
            {
                angle = Float.valueOf(jtfAngle.getText());
            }
            catch (NumberFormatException nfe)
            {
                errFlag = true;
            }
            try
            {
                iteration = Integer.valueOf(jtfIterations.getText());
            }
            catch (NumberFormatException nfe)
            {
                errFlag = true;
            }
            if (errFlag) {
                JOptionPane.showMessageDialog(this, "Error parameters.", "Error!", JOptionPane.ERROR_MESSAGE);
                return;
            }

            closeFlag = true;
            setVisible(false);

        }
        if (e.getActionCommand().equals(CANCEL_COMMAND)) {
            setVisible(false);
        }

    }

    public float getAngle() {
        return angle;
    }

    public int getIterations() {
        return iteration;
    }

}

